<template>
  <div class="donor-links" @mouseenter="isHover = true" @mouseleave="isHover = false">
    <div class="donor-links__container" :class="[modelValue?.width ? `container-content-${modelValue?.width}` : 'container-content']">
      <div class="donor-links__row row row-cols-1 row-cols-sm-2 row-cols-lg-3">
        <div v-for="link in modelValue?.data" :key="link.id">
          <!-- 如果链接包含 http 则使用 a 标签跳转 -->
          <a v-if="link.link && link.link.includes('http')" :href="link.link" class="donor-link btn">
            <div class="donor-link__text">
              {{ link.title }}
            </div>
            <span class="donor-link__icon"></span>
          </a>
          <!-- 否则使用 button 并 emit handleClick 事件 -->
          <button v-else-if="link.link" @click="handleClick(link.link)" class="donor-link btn">
            <div class="donor-link__text">
              {{ link.title }}
            </div>
            <span class="donor-link__icon"></span>
          </button>
        </div>
      </div>
    </div>

    <Operate v-if="!isPreview" v-model:show="isHover" @handle-edit="showSetting = true" @handle-delete="handleDelete" @handle-copy="handleCopy" @handle-paste="handlePaste"></Operate>
  </div>

  <Setting v-if="!isPreview" v-model:show="showSetting" :data="modelValue"></Setting>
</template>

<script lang="ts" setup>
import { ref } from 'vue'
import { randomString } from '@/utils/common'
import Setting from './setting.vue'
import Operate from '@/common/Operate/index.vue'

// 定义组件名称
defineOptions({
  name: 'DonorLinks'
})

const props = defineProps({
  modelValue: {
    type: Object,
    default: () => {}
  },

  // 是否预览
  isPreview: {
    type: Boolean,
    default: true
  }
})

const emit = defineEmits(['update:modelValue', 'handleDelete', 'handleCopy', 'handlePaste', 'handleClick'])

const handleInit = () => {
  if (props.modelValue && props.modelValue.data) return // 有数据不用初始化
  const data = {
    id: props.modelValue?.id || randomString(),
    type: 'DonorLinks',
    width: '',
    data: [
      {
        id: randomString(),
        title: '了解更多',
        link: 'https://www.baidu.com',
      },
      {
        id: randomString(),
        title: '了解更多',
        link: 'https://www.baidu.com',
      },
    ]
  }
  emit('update:modelValue', data)
} 

const isHover = ref<boolean>(false);
const showSetting = ref<boolean>(false);

const handleDelete = () => {
  emit('handleDelete', props.modelValue.id)
}

const handleCopy = () => {
  emit('handleCopy', props.modelValue)
}

const handlePaste = (type: string) => {
  emit('handlePaste', type)
}

const handleClick = (url: string) => {
  emit('handleClick', url)
}

handleInit()
</script>

<style lang="scss" scoped>
.row {
  --bs-gutter-x: 16px;
  --bs-gutter-y: 0;
  display: flex;
  flex-wrap: wrap;
  margin-top: calc(var(--bs-gutter-y)*-1);
  margin-right: calc(var(--bs-gutter-x)*-0.5);
  margin-left: calc(var(--bs-gutter-x)*-0.5)
}

.row>* {
  flex-shrink: 0;
  width: 100%;
  max-width: 100%;
  padding-right: calc(var(--bs-gutter-x)*0.5);
  padding-left: calc(var(--bs-gutter-x)*0.5);
  margin-top: var(--bs-gutter-y)
}

.col {
  flex: 1 0 0%
}

.row-cols-1>* {
  flex: 0 0 auto;
  width: 100%
}

@media(min-width: 576px) {
  .row-cols-sm-2>* {
    flex: 0 0 auto;
    width: 50%
  }
}

@media(min-width: 1024px) {
  .row-cols-lg-3>* {
    flex: 0 0 auto;
    width: 33.3333333333%
  }
}

.donor-links {
  position: relative;
}

/* 让 button 看起来像链接 */
.donor-links button.donor-link {
  background: none;
  border: none;
  padding: 0;
  cursor: pointer;
  text-decoration: none;
  color: inherit;
  font: inherit;
  width: 100%;
  text-align: left;
}

.donor-links__row {
  --bs-gutter-x: 12px;
  --bs-gutter-y: 8px;
}

.donor-links .donor-link {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background-color: #0032a0;
  background-color: var(--text-color--primary,#0032a0);
  padding: 6px 15px 6px 20px;
  min-height: 48px;
  border: none;
  border-radius: 4px;
  overflow: hidden;
  text-decoration: none;
  box-shadow: 0 2px 6px rgba(130,130,140,.12);
  position: relative;
  z-index: 1;
  white-space: nowrap;
}

.donor-links .donor-link:before {
  content: "";
  display: block;
  background: #0048e8;
  position: absolute;
  width: 200%;
  height: 500%;
  left: 50%;
  top: 50%;
  transform: translate(-50%,-50%);
  transition: all .3s ease-in-out;
  z-index: -1;
  -webkit-clip-path: circle(100% at -80% 90%);
  clip-path: circle(100% at -80% 90%);
}

.donor-links .donor-link:hover:before {
  -webkit-clip-path: circle(100% at 50% 50%);
  clip-path: circle(100% at 50% 50%);
}

.donor-links .donor-link:hover .donor-link__icon:before {
  transform: translate(-50%,-50%);
}

.donor-links .donor-link:hover .donor-link__icon:after {
  transform: translate(150%,-50%);
}

.donor-links .donor-link:hover .donor-link__icon:after,.donor-links .donor-link:hover .donor-link__icon:before {
  background-image: url();
  background-position: 50%;
  background-size: cover;
  background-repeat: no-repeat;
  background-color: hsla(0,0%,100%,0);
}

@media(max-width: 767.98px) {
  .donor-links .donor-link:hover .donor-link__icon:after,.donor-links .donor-link:hover .donor-link__icon:before {
    background-image:url();
    background-position: 50%;
    background-size: cover;
    background-repeat: no-repeat;
    background-color: hsla(0,0%,100%,0);
  }
}

.donor-links .donor-link:active:before {
  -webkit-clip-path: circle(100% at -80% 90%);
  clip-path: circle(100% at -80% 90%);
}

.donor-links .donor-link:active:before .btn-primary__icon:before {
  transform: translate(-260%,-50%);
}

.donor-links .donor-link:active:before .btn-primary__icon:after {
  transform: translate(-50%,-50%);
}

.donor-links .donor-link:active:before .btn-primary__icon:after,.donor-links .donor-link:active:before .btn-primary__icon:before {
  background-image: url();
  background-position: 50%;
  background-size: cover;
  background-repeat: no-repeat;
  background-color: hsla(0,0%,100%,0);
}

@media(max-width: 767.98px) {
  .donor-links .donor-link:active:before .btn-primary__icon:after,.donor-links .donor-link:active:before .btn-primary__icon:before {
    background-image:url();
    background-position: 50%;
    background-size: cover;
    background-repeat: no-repeat;
    background-color: hsla(0,0%,100%,0);
  }
}

.donor-links .donor-link__text {
  font-size: 14px;
  font-weight: 500;
  line-height: 1.285;
  letter-spacing: .04em;
  color: #fff;
  transition: color .3s ease-in-out;
  white-space: normal;
  text-align: center;
  flex-grow: 1;
}

@media(max-width: 1023.98px) {
  .donor-links .donor-link__text {
    line-height: 1.428;
  }
}

@media(max-width: 767.98px) {
  .donor-links .donor-link__text {
    line-height: 1.285;
  }
}

.donor-links .donor-link__icon {
  width: 18px;
  height: 18px;
  border-radius: 50%;
  overflow: hidden;
  background-color: #fff;
  position: relative;
  margin-left: 7px;
  transition: background-color .3s ease-in-out;
  flex-shrink: 0;
}

@media(max-width: 767.98px) {
  .donor-links .donor-link__icon {
    width: 12px;
    height: 12px;
    margin-left: 6px;
  }
}

.donor-links .donor-link__icon:after,.donor-links .donor-link__icon:before {
  content: "";
  width: 6px;
  height: 9px;
  position: absolute;
  top: 50%;
  left: 55%;
  background-image: url();
  background-position: 50%;
  background-size: cover;
  background-repeat: no-repeat;
  background-color: hsla(0,0%,100%,0);
  transition: transform .3s ease-in-out;
}

@media(max-width: 767.98px) {
  .donor-links .donor-link__icon:after,.donor-links .donor-link__icon:before {
    width:4.2px;
    height: 6px;
    background-image: url();
    background-position: 50%;
    background-size: cover;
    background-repeat: no-repeat;
    background-color: hsla(0,0%,100%,0);
  }
}

.donor-links .donor-link__icon:before {
  transform: translate(-260%,-50%);
}

.donor-links .donor-link__icon:after {
  transform: translate(-50%,-50%);
}
</style>
